package com.qf.studentmanager.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.studentmanager.model.Clazz;
import com.qf.studentmanager.model.RespBean;
import com.qf.studentmanager.model.RespPageBean;
import com.qf.studentmanager.service.ClazzService;
import jdk.nashorn.internal.ir.ObjectNode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/Clazz")
public class ClazzServlet extends HttpServlet {
    ClazzService clazzService = new ClazzService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        ObjectMapper objectMapper = new ObjectMapper();
        Clazz clazz = objectMapper.readValue(request.getInputStream(), Clazz.class);
        Integer integer = clazzService.addClazz(clazz);
        RespBean respBean = null;
        if (integer == 1){
            respBean = RespBean.ok("添加成功");
        }else if(integer == -2) {
            respBean = RespBean.error("班级重复，添加失败");
        }else {
            respBean = RespBean.error("添加失败");
        }
        String string = objectMapper.writeValueAsString(respBean);
        response.getWriter().write(string);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        ObjectMapper objectMapper = new ObjectMapper();
        String action = request.getParameter("action");
        if ("page".equals(action)){
            request.getRequestDispatcher("WEB-INF/jsp/class/clazzList.jsp").forward(request, response);
        }else if ("data".equals(action)){
            String page = request.getParameter("page");
            String rows = request.getParameter("rows");
            String sort = request.getParameter("sort");
            String oder = request.getParameter("oder");
            String gid = request.getParameter("gid");
            RespPageBean respPageBean = new RespPageBean();
             respPageBean =clazzService.getAllClazzByPage(page,rows,sort,oder,gid);
            String string = objectMapper.writeValueAsString(respPageBean);
            response.getWriter().write(string);
        }else if ("combobox".equals(action)){
            String cid = request.getParameter("cid");
            String gid = request.getParameter("gid");
            List<Clazz> clazzes = clazzService.getClazzByGid(gid);
            String string = objectMapper.writeValueAsString(clazzes);
            response.getWriter().write(string);
        }
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        ObjectMapper objectMapper = new ObjectMapper();
        RespBean respBean = null;
        String cid = req.getParameter("cid");

        Integer i = clazzService.deteteClazzByCid(cid);

        if (i == 1){
            respBean = RespBean.ok("删除成功");
        }else {
            respBean = RespBean.error("删除失败");
        }
        String string = objectMapper.writeValueAsString(respBean);
        resp.getWriter().write(string);
    }
}
